package com.lance.answer.gui.answer.gk.answer.lms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lance.answer.dto.Item;
import com.lance.answer.gui.answer.Question;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.answer.lms.dto.CourseInfo;
import com.lance.answer.gui.answer.gk.answer.lms.util.LmsUtil;
import com.lance.answer.util.HttpUtil;
import com.lance.answer.util.MD5Utils;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.*;

/**
 * <p>到处某门课程的所有题目<p>
 * <p>
 * <p>
 * <p>
 * <p>
 * 工程经济,size:1
 *
 * @author : fengx
 * @since JDK8.0
 * create by fengx on 2021/7/15 14:47
 */
public class GetAllCourseByJson {


    public static final String JS = "var video = document.getElementsByTagName(\"video\");if(video.length>0){ video[0].src = \"https://jvod.300hu.com/vod/product/a7d50669-f57b-4da9-8cf8-43d04372cab0/0411e189a55b41cfada876041fa7fdd7.mp4?source=2&h265=h265/18799/d7d0ab77577f4ea0bcf5bc794316045a.mp4\";}";


    public static void main(String[] args) throws Exception {
        String driverPath = "d:\\ynouAutoStudy\\chromedriver.exe";
        ChromeDriver driver = Util.getDriver(driverPath);
        String url = "https://menhu.pt.ouchn.cn/site/ouchnPc/index";
        driver.get(url);
        Thread.sleep(3000);


        String userName = "2211001201588";
        String pwd = "Ouchn@2021";
       // String pwd = "ouchn@2022";

        LmsUtil.login(driver,userName,pwd);
        // FIXME 要输入验证码

        Thread.sleep(2000);



       // FIXME TODO driver.get("https://lms.ouchn.cn/user/courses#/");
        driver.get("https://lms.ouchn.cn/user/courses#/");
        Thread.sleep(10000);

        CourseInfo courseInfo = getUserCourseInfo(driver);
        System.err.println(courseInfo);

        List<CourseInfo.CoursesDTO> courses = courseInfo.getCourses();

        Map<Long, String> courseMap = Maps.newHashMap();
        for(CourseInfo.CoursesDTO coursesDTO:courses){
            courseMap.put(coursesDTO.getId(),coursesDTO.getDisplayName());
        }



        File file = new File("libs//lmsCourseInfo//" + userName + ".json");

        FileUtils.write(file, JSONObject.toJSONString(courseMap, SerializerFeature.PrettyFormat), "utf-8");

    }


    private static CourseInfo getUserCourseInfo(WebDriver driver){
        // FIXME  page_size =10 是有问题的。
        String getUrl = "https://lms.ouchn.cn/api/my-courses?conditions=%7B%22status%22:%5B%22ongoing%22%5D,%22keyword%22:%22%22%7D&fields=id," +
                "name,course_code,department(id,name),grade(id,name),klass(id,name),course_type,cover,small_cover," +
                "start_date,end_date,is_started,is_closed,academic_year_id,semester_id,credit,compulsory,second_name," +
                "display_name,created_user(id,name),org(is_enterprise_or_organization),org_id,public_scope,course_attributes" +
                "(teaching_class_name,copy_status,tip,data),audit_status,audit_remark,can_withdraw_course,imported_from," +
                "allow_clone,is_instructor,is_team_teaching,academic_year(id,name),semester(id,name)," +
                "instructors(id,name,email,avatar_small_url),is_master,is_child,has_synchronized,master_course(name)" +
                "&page=1&page_size=10";

        String cookie = Util.getCookie(driver.manage().getCookies());
        Map<String, String> heads = Maps.newHashMap();
        heads.put("referer", "https://lms.ouchn.cn/user/courses");
        heads.put(":method", "GET");
        heads.put(":authority", "lms.ouchn.cn");
        heads.put("cookie", cookie);
        heads.put(":path", "/api/my-courses?conditions=%7B%22status%22:%5B%22ongoing%22%5D,%22keyword%22:%22%22%7D&fields=id,name,course_code,department(id,name),grade(id,name),klass(id,name),course_type,cover,small_cover,start_date,end_date,is_started,is_closed,academic_year_id,semester_id,credit,compulsory,second_name,display_name,created_user(id,name),org(is_enterprise_or_organization),org_id,public_scope,course_attributes(teaching_class_name,copy_status,tip,data),audit_status,audit_remark,can_withdraw_course,imported_from,allow_clone,is_instructor,is_team_teaching,academic_year(id,name),semester(id,name),instructors(id,name,email,avatar_small_url),is_master,is_child,has_synchronized,master_course(name)&page=1&page_size=10");
        heads.put("accept", "application/json, text/plain, */*");


        String result = null;
        try {
            result = HttpUtil.get(getUrl,cookie,heads);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSON.parseObject(result,CourseInfo.class);

    }





}
